package cn.foodProject.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import cn.foodProject.R;
import cn.foodProject.Utils.SpUtils;
import cn.foodProject.gsonBean.CommentGsonBean;
import cn.foodProject.gsonBean.LoginGsonBean;

public class CommentListAdapter extends BaseAdapter {

    private List<CommentGsonBean.Comment> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;


    public CommentListAdapter(Context context, List<CommentGsonBean.Comment> objects) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects = objects;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CommentGsonBean.Comment getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_list_comment, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((CommentGsonBean.Comment)getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(CommentGsonBean.Comment object, ViewHolder holder) {
        holder.tvDate.setText(object.getPostdate());
        holder.tvUsername.setText(holder.user.getName());
        holder.tvComment.setText(object.getComment());
    }

    protected class ViewHolder {
        private TextView tvUsername;
        private TextView tvDate;
        private TextView tvComment;
        LoginGsonBean.User user;

        public ViewHolder(View view) {
            tvUsername = (TextView) view.findViewById(R.id.tv_username);
            tvDate = (TextView) view.findViewById(R.id.tv_date);
            tvComment = (TextView) view.findViewById(R.id.tv_comment_details);
             user = (LoginGsonBean.User) SpUtils.getBean(context,"user", LoginGsonBean.User.class);
        }
    }
}
